package com.example.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;

public class CameraAlbumActivity extends AppCompatActivity {
    ImageView image,camera_back;
    private File currentFile;
    public static final int CHOOSE_PHOTO = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_album);
        //
        initView();
    }

    private void initView() {
        image = findViewById(R.id.image1);
//        camera_back = findViewById(R.id.camera_back);
//        camera_back.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(CameraAlbumActivity.this,CateActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        });
        Button chooseFromAlbum = findViewById(R.id.choose_from_album);
        chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
    /*
    * 启动系统相机
    * @param view
    * */
    public void takePhoto(View view){
        //创建存放图片的文件夹
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory(),"myphoto");
        //如果文件不存在，则创建一个文件夹
        if(!file.exists()){
            file.mkdir();
        }

        currentFile = new File(file,System.currentTimeMillis()+".jpg");
        if (!currentFile.exists()){
            try {
                currentFile.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }

        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentFile));
        startActivityForResult(intent,1);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==1){
                image.setImageURI(Uri.fromFile(currentFile));
            }
    }
}
